/*************************************************************************
  > File Name: userdat.c
  > Author: QiyangXia
  > Mail: 834061456@qq.com 
  > Created Time: 2022年01月24日 星期一 13时02分53秒
 ************************************************************************/
#include "userdat.h"


/*
   struct DATA
   {
   int id;
   char name[20];
   char passwd[20];
   };
   typedef struct user
   {
   struct DATA data;
   struct user *next;
   }USR,* Pusr;

   void login();
   int proc();
   void add();
   Pusr read_file();
   Pusr search(int id);
   void write_file(Pusr head);
   */
char user_name[25]="hello";

void login()
{
	int i=1;
	while(i<=3)
	{
		char name[20];
		char passwd[20];
		printf("\033[32m------------------------------------\n");
		printf("请输入用户名:");
		scanf("%s",name);
		while(getchar()!='\n'){}
		printf("请输入密码:");
		printf("\033[8m");
		scanf("%s",passwd);
		while(getchar()!='\n'){}
		printf("\033[0m\n");
		Pusr head=read_file();
		if(head==NULL)
		{
			break;
		}
		Pusr p=head->next;
		while(p!=NULL)
		{
			if(strcmp(p->data.passwd,passwd)==0 && strcmp(p->data.name,name)==0)
			{
				strcpy(user_name,p->data.name);
				printf("\033[32m登陆成功！！！！\n\n\n");
				printf("请按任意键登录\n");
				printf("--------------------------------------\033[0m\n");
				getchar();
				dloop(head,p);
				return ;
			}
			p=p->next;
		}
		printf("\033[32m您输入的用户名或者密码有误!!\n");
		printf("--------------------------------------\033[0m\n");
		i++;
	}
	if(i>3)
	{
		printf("\033[031m三次输入错误，请等待5s\033[0m\n");
		proc();
	}
}


void add()
{
	Pusr head=read_file();
	if(head==NULL)
	{
		head=(Pusr)malloc(sizeof(USR));
		head->next=NULL;
	}
	Pusr p=NULL;
	Pusr temp=(Pusr)malloc(sizeof(USR));

	while(1)
	{
		int id;
		printf("\033[32m------------------------------------\n");
		printf("请输入您要添加的id:");
		scanf("%d",&id);
		while(getchar()!='\n'){}
		p=search(id);
		if(p==NULL)
		{
			temp->data.id=id;
			break;
		}
		else
		{
			printf("您要添加的id已存在，请重新输入\n");

		}
	}
	printf("请输入您要添加的name:");
	scanf("%s",temp->data.name);
	while(getchar()!='\n'){}
	printf("请输入您要添加的passwd:");
	scanf("%s",temp->data.passwd);
	while(getchar()!='\n'){}
	temp->next=head->next;
	head->next=temp;

	printf("添加成功\n\n\n");
	printf("--------------------------------------\033[0m\n");

	write_file(head);
}

Pusr search(int id)
{
	Pusr head=read_file();
	if(head==NULL||head->next==NULL)
	{
		return NULL;
	}
	Pusr p=head->next;
	while(p!=NULL)
	{
		if(p->data.id==id)
		{
			return p;
		}

		p=p->next;
	}
	return p;
}

void write_file(Pusr head)
{
	FILE *fp;
	if((fp=fopen("user.txt","w"))==NULL)
	{
		printf("\033[32m打开文件失败\033[0m\n");
		return ;
	}
	if(head==NULL||head->next==NULL)
	{
		printf("\033[32m系统无用户\n");
		return ;
	}
	head=head->next;
	while(head!=NULL)
	{
		fprintf(fp,"%d\t%s\t%s\n",head->data.id,head->data.name,head->data.passwd);
		head=head->next;
	}
	fclose(fp);
}

Pusr read_file()
{
	FILE *fp;
	if((fp=fopen("user.txt","r"))==NULL)
	{
		printf("\033[32m系统无用户\n");
		return NULL;
	}
	Pusr head=(Pusr)malloc(sizeof(USR));
	head->next=NULL;
	Pusr temp=(Pusr)malloc(sizeof(USR));
	while((fscanf(fp,"%d\t%s\t%s\n",&temp->data.id,temp->data.name,temp->data.passwd))>0)
	{
		temp->next=head->next;
		head->next=temp;
		temp=NULL;
		temp=(Pusr)malloc(sizeof(USR));
	}
	free(temp);
	temp=NULL;
	fclose(fp);
	return head;
}

int proc()
{
	int cont = 5;
	while(cont>=0)
	{
		printf("\033[31m%2d s\r\033[0m",cont);
		fflush(stdout);
		cont--;
		sleep(1);
	}
}
